//
//  LGMainInspectItemController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/19.
//  核心检查项

#import "LGMainInspectItemController.h"
//检查项趋势
#import "LGInspectChartController.h"
//区头
#import "LGPatientAttendHeader.h"
#import "LGPatientAttendHeaderCell.h"
//检查结果
#import "LGPatientAttendNormalCell.h"
//诊断文本
#import "LGPatientAttendStringCell.h"
//心脏彩超
#import "LGPatientAttendHeartUltrasoundCell.h"
//纽约心功能分级
#import "LGPatientAttendClassificationCell.h"
//评估
#import "LGPatientAttendEvaluateCell.h"
#import "LGPatientAttendModel.h"
@interface LGMainInspectItemController ()<UITableViewDataSource,UITableViewDelegate,LGPatientAttendHeaderDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UILabel *dateLabel;
/**   */
@property (nonatomic , strong)UILabel *rangeLabel;
/**   */
@property (nonatomic , strong)NSMutableArray *sectionArray;
/**  血常规 */
@property (nonatomic , strong)NSArray *reportArray1;
/**  肝功 */
@property (nonatomic , strong)NSArray *reportArray2;
/**  肾功 */
@property (nonatomic , strong)NSArray *reportArray3;
/**  血脂 */
@property (nonatomic , strong)NSArray *reportArray4;
/**  心肌标志物 */
@property (nonatomic , strong)NSArray *reportArray5;
/**  心电图诊断结果 */
@property (nonatomic , copy)NSString *reportString6;
/**  心脏彩超 */
@property (nonatomic , strong)NSDictionary *reportDic7;
/**   */
@property (nonatomic , strong)NSArray *dateArray;
/**   */
@property (nonatomic , copy)NSString *currentDate;
@end

@implementation LGMainInspectItemController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"核心指标汇总";
    NSArray *tempArray = @[@{@"title":@"血常规",@"opened":@(YES)},@{@"title":@"生化",@"opened":@(YES)},@{@"title":@"血脂",@"opened":@(YES)},@{@"title":@"心肌标志物",@"opened":@(YES)},@{@"title":@"心电图",@"opened":@(YES)},@{@"title":@"心脏彩超",@"opened":@(YES)}];
    self.sectionArray = [LGPatientAttendModel mj_objectArrayWithKeyValuesArray:tempArray];
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.care_plan/careplanVisitReport" parameters:@{@"patient_id":self.patientid,@"visit_date":self.fullowDate} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dateLabel.text = [NSString stringWithFormat:@"生产时间：%@复查",returnData[@"date"][@"visit_date"]];
            self.rangeLabel.text = [NSString stringWithFormat:@"根据%@至%@期间的检查生成",returnData[@"date"][@"range"][@"start"],returnData[@"date"][@"range"][@"end"]];
            self.reportArray1 = returnData[@"report1"];
            self.reportArray2 = returnData[@"report2"];
            self.reportArray3 = returnData[@"report3"];
            self.reportArray4 = returnData[@"report4"];
            self.reportArray5 = returnData[@"report5"];
            self.reportString6 = returnData[@"report6"][@"content"];
            self.reportDic7 = returnData[@"report7"];
            LGPatientAttendModel *model = self.sectionArray[4];
            model.opened = (self.reportString6.length>0)?YES:NO;
            [self.sectionArray replaceObjectAtIndex:4 withObject:model];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}


-(void)openCloseHeaderView:(BOOL)open section:(NSInteger)section{
    LGPatientAttendModel *model = self.sectionArray[section];
    model.opened = open;
    [self.sectionArray replaceObjectAtIndex:section withObject:model];
//    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    LGPatientAttendModel *model = self.sectionArray[section];
    if(section == 0){
        //血常规
        return (model.opened == YES)?self.reportArray1.count+1:0;
        
    }else if (section == 1){
        //生化
        NSInteger count = 0;
        if(self.reportArray2.count>0){
            count += self.reportArray2.count+1;
        }
        
        if(self.reportArray3.count>0){
            count += self.reportArray3.count+1;
        }
        return (model.opened == YES)?count:0;;
        
    }else if (section == 2){
        //血脂
        return (model.opened == YES)?self.reportArray4.count+1:0;
        
    }else if (section == 3){
        //心肌标志物
        return (model.opened == YES)?self.reportArray5.count+1:0;
        
    }else if (section == 4){
        //心电图
        return (model.opened == YES && self.reportString6.length>0)?1:0;
        
    }else if (section == 5){
        //心脏彩超
        return (model.opened == YES)?1:0;
        
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        //血常规
        if(indexPath.row == 0){
            LGPatientAttendHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
            headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
            headerCell.title = @"";
            return headerCell;
        }else{
            LGPatientAttendNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
            normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
            normalCell.dataDic = self.reportArray1[indexPath.row-1];
            return normalCell;
        }
    }else if(indexPath.section == 1){
        if(self.reportArray2.count>0){
            if(indexPath.row == 0 || indexPath.row == self.reportArray2.count+1){
                LGPatientAttendHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
                headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
                headerCell.title = (indexPath.row == 0)?@"肝功":@"肾功";
                return headerCell;
                
            }else{
                LGPatientAttendNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
                normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
                if(indexPath.row-1<self.reportArray2.count){
                    normalCell.dataDic = self.reportArray2[indexPath.row-1];
                }else{
                    normalCell.dataDic = self.reportArray3[indexPath.row-self.reportArray2.count-2];
                }
                return normalCell;
            }
        }else{
            if(indexPath.row == 0){
                LGPatientAttendHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
                headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
                headerCell.title = @"肾功";
                return headerCell;
                
            }else{
                LGPatientAttendNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
                normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
                normalCell.dataDic = self.reportArray3[indexPath.row-1];
                return normalCell;
            }
        }
        
    }else if (indexPath.section == 2){
        //血脂
        if(indexPath.row == 0){
            LGPatientAttendHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
            headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
            headerCell.title = @"";
            return headerCell;
        }else{
            LGPatientAttendNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
            normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
            normalCell.dataDic = self.reportArray4[indexPath.row-1];
            return normalCell;
        }
    }else if (indexPath.section == 3){
        //心肌标志物
        if(indexPath.row == 0){
            LGPatientAttendHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
            headerCell.selectionStyle = UITableViewCellSelectionStyleNone;
            headerCell.title = @"";
            return headerCell;
        }else{
            LGPatientAttendNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
            normalCell.selectionStyle = UITableViewCellSelectionStyleNone;
            normalCell.dataDic = self.reportArray5[indexPath.row-1];
            return normalCell;
        }
        
    }else if (indexPath.section == 4){
        //心电图
        LGPatientAttendStringCell *stringCell = [tableView dequeueReusableCellWithIdentifier:@"stringCell" forIndexPath:indexPath];
        stringCell.selectionStyle = UITableViewCellSelectionStyleNone;
        stringCell.content = self.reportString6;
        return stringCell;
        
    }else if (indexPath.section == 5){
        //心脏彩超
        LGPatientAttendHeartUltrasoundCell *ultrasoundCell = [tableView dequeueReusableCellWithIdentifier:@"ultrasoundCell" forIndexPath:indexPath];
        ultrasoundCell.selectionStyle = UITableViewCellSelectionStyleNone;
        ultrasoundCell.dataDic = self.reportDic7;
        return ultrasoundCell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //检查项趋势
    NSString *itemCode = @"";
    NSString *patientNum = @"";
    NSString *patientid = @"";
    if(indexPath.section == 0 && indexPath.row > 0){
        NSDictionary *tempDic = self.reportArray1[indexPath.row-1];
        itemCode = LGNSString(tempDic[@"item_code"]);
        patientNum = LGNSString(tempDic[@"hospital_bano"]);
        patientid = LGNSString(tempDic[@"patient_id"]);
        
    }else if (indexPath.section == 1 && indexPath.row > 0){
        if(self.reportArray2.count>0 && indexPath.row != 0 && indexPath.row != self.reportArray2.count+1){
            if(indexPath.row-1<self.reportArray2.count){
                NSDictionary *tempDic = self.reportArray2[indexPath.row-1];
                itemCode = LGNSString(tempDic[@"item_code"]);
                patientNum = LGNSString(tempDic[@"hospital_bano"]);
                patientid = LGNSString(tempDic[@"patient_id"]);
            }else{
                NSDictionary *tempDic = self.reportArray3[indexPath.row-self.reportArray2.count-2];
                itemCode = LGNSString(tempDic[@"item_code"]);
                patientNum = LGNSString(tempDic[@"hospital_bano"]);
                patientid = LGNSString(tempDic[@"patient_id"]);
            }
        }else if(indexPath.row != 0){
            NSDictionary *tempDic = self.reportArray3[indexPath.row-1];
            itemCode = LGNSString(tempDic[@"item_code"]);
            patientNum = LGNSString(tempDic[@"hospital_bano"]);
            patientid = LGNSString(tempDic[@"patient_id"]);
        }
    }else if (indexPath.section == 2 && indexPath.row > 0){
        NSDictionary *tempDic = self.reportArray4[indexPath.row-1];
        itemCode = LGNSString(tempDic[@"item_code"]);
        patientNum = LGNSString(tempDic[@"hospital_bano"]);
        patientid = LGNSString(tempDic[@"patient_id"]);
        
    }else if (indexPath.section == 3 && indexPath.row > 0){
        NSDictionary *tempDic = self.reportArray5[indexPath.row-1];
        itemCode = LGNSString(tempDic[@"item_code"]);
        patientNum = LGNSString(tempDic[@"hospital_bano"]);
        patientid = LGNSString(tempDic[@"patient_id"]);
    }
    
    if(itemCode.length>0){
        LGInspectChartController *controller = [[LGInspectChartController alloc]init];
        controller.code = itemCode;
        controller.patientid = patientid;
        controller.patientNum = patientNum;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(50);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGPatientAttendModel *model = self.sectionArray[section];
    LGPatientAttendHeader *headerView = [[LGPatientAttendHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
    headerView.delegate = self;
    headerView.section = section;
    headerView.title = model.title;
    headerView.opened = model.opened;
    
    BOOL finished = NO;
    if(section == 0 && self.reportArray1.count>0){
        //血常规
        finished = YES;
        
    }else if (section == 1){
        //生化
        NSInteger count = 0;
        if(self.reportArray2.count>0){
            count += self.reportArray2.count+1;
        }
        
        if(self.reportArray3.count>0){
            count += self.reportArray3.count+1;
        }
        
        finished = (count>0)?YES:NO;
        
    }else if (section == 2 && self.reportArray4.count>0){
        //血脂
        finished = YES;
        
    }else if (section == 3 && self.reportArray5.count>0){
        //心肌标志物
        finished = YES;
        
    }else if (section == 4 && self.reportString6.length>0){
        //心电图
        finished = YES;
        
    }else if (section == 5 && [self.reportDic7 allKeys].count>0){
        //心脏彩超
        finished = YES;
        
    }
    headerView.finished = finished;
//    [headerView.checkBtn addTarget:self action:@selector(checkHomeMonitoringAction) forControlEvents:UIControlEventTouchUpInside];
    return headerView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(20);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(20))];
    footerView.backgroundColor = [UIColor clearColor];
    footerView.bottomMargin = viewPix(10);
    footerView.shadowView.layer.cornerRadius = 15;
    return footerView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientAttendHeaderCell class] forCellReuseIdentifier:@"headerCell"];
        [_tableView registerClass:[LGPatientAttendNormalCell class] forCellReuseIdentifier:@"normalCell"];
        [_tableView registerClass:[LGPatientAttendStringCell class] forCellReuseIdentifier:@"stringCell"];
        [_tableView registerClass:[LGPatientAttendHeartUltrasoundCell class] forCellReuseIdentifier:@"ultrasoundCell"];
        [_tableView registerClass:[LGPatientAttendClassificationCell class] forCellReuseIdentifier:@"classificationCell"];
        [_tableView registerClass:[LGPatientAttendEvaluateCell class] forCellReuseIdentifier:@"evaluateCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = viewPix(35);
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
    }
    return _tableView;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _dateLabel.frame = CGRectMake(viewPix(18), viewPix(15), Screen_W-viewPix(36), viewPix(20));
    }
    return _dateLabel;
}

-(UILabel *)rangeLabel{
    if(!_rangeLabel){
        _rangeLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
        _rangeLabel.frame = CGRectMake(viewPix(18), viewPix(38), Screen_W-viewPix(36), viewPix(20));
    }
    return _rangeLabel;
}

-(UIView *)tableHeaderView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(60))];
    [baseView addSubview:self.dateLabel];
    [baseView addSubview:self.rangeLabel];
    return baseView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
